package com.gd.iot.energy.service.superadmin.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gd.iot.energy.consts.SystemConsts;
import com.gd.iot.energy.dao.superadmin.AppAdminDetailPOMapper;
import com.gd.iot.energy.po.superadmin.AppAdminDetailPO;
import com.gd.iot.energy.service.superadmin.AppAdminDetailService;
import com.gd.iot.energy.util.CurrentUserUtil;
import com.gd.iot.energy.util.CustomBigDecimalUtil;
import com.gd.iot.energy.util.EnergyTypeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户详细信息管理
 */
@Service
public class AppAdminDetailServiceImpl extends ServiceImpl<AppAdminDetailPOMapper, AppAdminDetailPO> implements AppAdminDetailService {

    @Resource
    private AppAdminDetailPOMapper appAdminDetailPOMapper;

    @Override
    public List<AppAdminDetailPO> getAppAdminDetailPOByRoleIds(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return new ArrayList<>(0);
        }
        QueryWrapper<AppAdminDetailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        queryWrapper.in("admin_id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        List<AppAdminDetailPO> list = list(queryWrapper);
        return list;
    }

    @Override
    public boolean saveDetail(String balance, String overmoney, Long adminId) {
        AppAdminDetailPO appAdminDetailPO = new AppAdminDetailPO();
        appAdminDetailPO.setBalance(CustomBigDecimalUtil.parseBigDecimal(balance));
        appAdminDetailPO.setOverMoney(CustomBigDecimalUtil.parseBigDecimal(overmoney));
        appAdminDetailPO.setAdminId(adminId);
        appAdminDetailPO.setOperatorId(CurrentUserUtil.getUserInfo().getId());
        appAdminDetailPO.setCreateDate(new Date());
        appAdminDetailPO.setModifyDate(new Date());
        appAdminDetailPO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        appAdminDetailPO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        return appAdminDetailPOMapper.insert(appAdminDetailPO) > 0;
    }

    @Override
    public boolean updateDetail(String balance, String overmoney, Long adminId) {
        AppAdminDetailPO appAdminDetailPO = new AppAdminDetailPO();
        appAdminDetailPO.setBalance(CustomBigDecimalUtil.parseBigDecimal(balance));
        appAdminDetailPO.setOverMoney(CustomBigDecimalUtil.parseBigDecimal(overmoney));
        appAdminDetailPO.setAdminId(adminId);
        appAdminDetailPO.setOperatorId(CurrentUserUtil.getUserInfo().getId());
        appAdminDetailPO.setCreateDate(new Date());
        appAdminDetailPO.setModifyDate(new Date());
        appAdminDetailPO.setEnergyType(SystemConsts.EnergyTypeEnum.WATER.value);
        appAdminDetailPO.setIsDel(SystemConsts.BooleanEnum.FALSE.value);
        QueryWrapper<AppAdminDetailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        return saveOrUpdate(appAdminDetailPO, queryWrapper);
    }

    @Override
    public AppAdminDetailPO getOneByAdminId(Integer adminId) {
        QueryWrapper<AppAdminDetailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_id", adminId);
        queryWrapper.eq("is_del", SystemConsts.BooleanEnum.FALSE.value);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        return appAdminDetailPOMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean deleteByAdminIds(List<Integer> ids) {
        QueryWrapper<AppAdminDetailPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("admin_id", ids);
        EnergyTypeUtil.injectEnergyTypeWrapper(queryWrapper);
        AppAdminDetailPO appAdminDetailPO = new AppAdminDetailPO();
        appAdminDetailPO.setIsDel(SystemConsts.BooleanEnum.TRUE.value);
        int num = appAdminDetailPOMapper.update(appAdminDetailPO, queryWrapper);
        return num > 0;
    }
}
